<?php
class UserAction extends Action{

	public function index() {
		$user = D("User");
		//echo $user->count(); 统计一共多少数据
		//$date = $user->getByUsername('autowang'); 根据某个字段直接返回该条数据
		$keyword = trim($_POST["keywords"]);
		$type = $_POST["type"];
		$this->assign("keyword",$keyword);
		$this->assign("type",$type);

		if(!empty($_POST["keywords"]) && !empty($type)){
			$where["$type"] = array("like","%".$keyword."%");
			$list = $user->where($where)->select();
		}

		if (empty($keyword)){
			//$where['id']=array(array('egt',1),array('elt',1),'and');
			$list = $user->select();
		}else{

		}
		$this->assign('ulist',$list);
		$this -> display();
	}



	public function add() {
		$this -> display();
	}

	/*
	 *	插入用户
	 */
	public function insert(){
		$user = D("User");
		$data = $user->create();
		dump($data);
		if ($data){
			if (false !== $user->add($data)){
				$userid = $user->getLastInsID();//获取插入数据的id（只能用于自增id）
				echo "创建成功,用户id为：".$userid;
			}else{
				echo "创建失败！错误信息为：".$user->getDbError();//获取db错误信息
			}
			$user->add($data);
		}else{
			echo $user->getError();
		}
		$this->display();
	}

	public function edit(){
		$id = $_GET['id'];
		if (!empty($id)){
			$user = D("User");
			$data = $user->getById($id);
			$this->assign('udata',$data);
		}else{
			echo "请选择编辑用户";
			return ;
		}
		$this -> display();
	}
	
	/*
	 * 更新用户
	 */
	
	public function update(){
		$user = D("User");
		$data = $user->create();//取到返回true
		dump($data);
		if ($data){
			if (!empty($data["id"])){
				if (!false == $user->save($data)){
					echo '更新成功';
				}else{
					echo "更新失败".$user->getDbError();
				}
			}else {
				echo $user->getError();
			}
		}
	}



}